package com.course.business.controller.admin;

import com.course.business.controller.BaseController;
import com.course.common.response.BaseResponse;
import com.course.model.dto.ChapterDto;
import com.course.model.dto.ChapterPageDto;
import com.course.model.dto.PageDto;
import com.course.model.service.ChapterService;
import com.course.model.util.ValidatorUtil;
import com.google.gson.Gson;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @PROJECT_NAME: business
 * @DESCRIPTION:
 * @Author: 涂玄武
 * @DATE: 2021/4/9 10:12
 */
@RestController
@RequestMapping("/admin/chapter")
public class ChapterController extends BaseController {

    @Resource
    private ChapterService chapterService;

    public static final String BUSINESS_NAME = "大章";

    private static Gson gson = new Gson();

    /**
     * txw 2021-04-09 获取所有大章内容
     *
     * @return
     */
    @RequestMapping("/getChapterList")
    public BaseResponse list(@RequestBody ChapterPageDto chapterPageDto) {
        ValidatorUtil.require(chapterPageDto.getCourseId(),"课程ID");
        BaseResponse response = new BaseResponse().sucess();
        response.setData(chapterService.list(chapterPageDto));
        return response;
    }

    /**
     * txw 2021-04-12 新增大章
     * @param chapterDto
     * @return
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody @Validated ChapterDto chapterDto, BindingResult result){
        //参数校验方式一：@Validated注解+BindingResult
        ValidatorUtil.checkResult(result);
        BaseResponse response = new BaseResponse().sucess();
        chapterService.saveChapter(chapterDto);
        //参数校验方式二：全局异常捕获
        //保存校验
//        ValidatorUtil.require(chapterDto.getName(),"名称");
//        ValidatorUtil.require(chapterDto.getCourseId(),"课程ID");
//        ValidatorUtil.length(chapterDto.getCourseId(),"课程ID",1,8);
        return response;
    }

    /**
     * txw 2021-04-12 删除大章
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id){
        BaseResponse response = new BaseResponse().sucess();
        chapterService.deleteChapter(id);
        return response;
    }

}
